//
//  ViewController.m
//  反应能力测试游戏
//
//  Created by 王国栋 on 15/9/1.
//  Copyright (c) 2015年 xiaobai. All rights reserved.
//

#import "SecViewController.h"
int ItemSum= 25;
@interface SecViewController ()
@property (nonatomic,strong)UIButton *btn_restart;
@property (nonatomic,strong)UIButton *btn_return;
@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic,copy)NSMutableArray *Arr_Buttons;
@property (nonatomic,assign)int step;
@property (nonatomic,assign)double time;
@property (nonatomic,strong)UITextField*txt_time;
@property (nonatomic,assign)double best;

@end

@implementation SecViewController
 - (void)viewDidLoad {
    [super viewDidLoad];
     
    self.txt_time = [[UITextField alloc]initWithFrame:CGRectMake(150, 480, 100, 60)];
    self.txt_time.text=[NSString stringWithFormat:@"%.2f",_time];
    self.txt_time.textColor=[UIColor greenColor];
     self.btn_restart=[[UIButton alloc]initWithFrame:CGRectMake(80, 480, 60, 60)];
     [self.btn_restart setTitle:@"开始" forState:UIControlStateNormal];
     [self.btn_restart setBackgroundImage:[UIImage imageNamed:@"smallboy"] forState:UIControlStateNormal];
     [self.btn_restart.layer setCornerRadius:30];
     [self.btn_restart.layer setMasksToBounds:YES];
     self.btn_restart.titleLabel.textColor = [UIColor blackColor];
     [_btn_restart addTarget:self action:@selector(startgame) forControlEvents:UIControlEventTouchUpInside];
     
     self.view.backgroundColor=[UIColor lightTextColor];
    [self.view addSubview:_txt_time];
    [self.view addSubview:_btn_restart];

}
//给按钮的标签赋值
-(NSMutableArray *)randNum
{
    NSMutableArray *arr=[NSMutableArray array];
    srand((unsigned)time(NULL));
    int n;
    while ([arr count]!=ItemSum)
    {
        int i=0;
        n=rand()%ItemSum+1;
        for (i=0; i<[arr count]; i++)
        {
            if (n==[[arr objectAtIndex:i]intValue]) {
                break;
            }
        }
        if ([arr count]==i)
        {
            [arr addObject:[NSString stringWithFormat:@"%d",n]];
        }
        
    }
    return  arr;
}
-(void)touchbutton:(UIButton *)sender
{
    if ([[[sender titleLabel]text]intValue]==self.step) {
        self.step++;
        [sender setEnabled:NO];
        [sender setBackgroundColor:[UIColor grayColor]];
    }
    if (self.step==2) {
         self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timer_method) userInfo:nil repeats:YES];
    }
    if (self.step==ItemSum+1) {
        
        [self.timer invalidate];
        double time_use=self.txt_time.text.doubleValue;
        double time_record;
        if ([_Arr_Buttons count]==16)
        {
            
            NSString *path=[[NSBundle mainBundle]pathForResource:@"score.plist" ofType:nil];
            NSDictionary *dict=[NSDictionary dictionaryWithContentsOfFile:path];
            time_record=[[dict allValues][0]doubleValue];
            if (time_record>time_use) {
                
                NSLog(@"%.2f %.2f",time_use,time_record);
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"恭喜" message:@"你创造了新的记录,请输入姓名"delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
                alert.tag=10;
                [alert show];
            }
            else
            {
                NSString *str=[NSString stringWithFormat:@"你的最终用时%@秒",self.txt_time.text];
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"成绩" message:str delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alert show];
            }
        }
        else
        {
            NSString *str=[NSString stringWithFormat:@"你的最终用时%@秒",self.txt_time.text];
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"成绩" message:str delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
       
    }
}

-(void)timer_method
{
     _time+=0.01;
    [_txt_time setText:[NSString stringWithFormat:@"%.2f",_time]];
}
-(void)startgame
{
    UIActionSheet *as=[[UIActionSheet alloc]initWithTitle:@"选择模式" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"简单",@"中等",@"困难",nil];
    //要先清除屏幕上的原有的控件
    for(UIButton *btn in _Arr_Buttons)
    {
        [btn removeFromSuperview];
    }
    [as showInView:self.view];
    
}
//监听alertview 输入成绩
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==10&&buttonIndex == 0) {
        
        NSString *path=[[NSBundle mainBundle]pathForResource:@"score.plist" ofType:nil];
        NSDictionary *dict=@{[alertView textFieldAtIndex:0].text:_txt_time.text};
        [dict writeToFile:path atomically:YES];
    }
}
// actionsheet的监听事件
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ld",buttonIndex);
    self.step=1;
    self.time=0;
    _Arr_Buttons=[NSMutableArray array];
    CGFloat marginX=10;//距离屏幕左边的距离
    CGFloat marginY=80;//距离屏幕上方距离
    CGFloat itemW=45;
    CGFloat itemH=45;
    int column;//每一行个数
    CGFloat itemMarginY=30;//人为定义
    if (buttonIndex == 0) {
        ItemSum=9;
        }
    else if(buttonIndex == 1)
    {
        ItemSum=16;
    }
    else if(buttonIndex==2)
    {
        ItemSum=25;
    }
    column=(int)sqrt(ItemSum);
    CGFloat itemMarginX=(self.view.frame.size.width-2*marginX-column*itemW)/(column-1);//元素间的横间距
    NSArray *Arr_num=[NSArray arrayWithArray:[self randNum]];
    for (int i=0; i<ItemSum; i++) {
        //创建按钮
        UIButton *btn=[[UIButton alloc]init];
        btn.backgroundColor=[UIColor greenColor];
        btn.frame=CGRectMake(marginX+(itemW+itemMarginX)*(i%column), marginY+(itemH+itemMarginY)*(i/column), itemW, itemW);
        
         [btn setTitle:Arr_num[i] forState:UIControlStateNormal];
         btn.layer.cornerRadius=btn.frame.size.width/2;//设置圆
         [btn setBackgroundImage:[UIImage imageNamed:@"smile"] forState:UIControlStateDisabled];
         btn.layer.borderColor=[[UIColor purpleColor]CGColor];//设置边的颜色
         btn.layer.masksToBounds=YES;
        
        [btn addTarget:self action:@selector(touchbutton:) forControlEvents:UIControlEventTouchUpInside];
        //添加随机数
        [_Arr_Buttons addObject:btn];
        [self.view addSubview:btn];
    }

}
@end
